Skip to content

Support freeze: pre-populate @normalized before locking#33

Merged
nertzy merged 1 commit into
minad:masterfrom
nertzy:support-freeze
Jun 17, 2026
Merged

Support freeze: pre-populate @normalized before locking#33
nertzy merged 1 commit into
minad:masterfrom
nertzy:support-freeze

Conversation

@nertzy

@nertzy nertzy commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Problem

Frozen Unit instances raise FrozenError on the first comparison call. normalize lazily memoizes via @normalized ||= dup.normalize!; when the object is frozen that write fails. Since <=> calls normalize, every comparison operator (<, >, >=, <=, ==) is affected.

Fix

Override freeze to pre-populate @normalized while the object is still mutable:

def freeze
  normalize  # populate @normalized while still mutable
  super
end

Once @normalized is set the ||= short-circuits on every subsequent call, so the frozen instance is safe to compare indefinitely.

Tests

  • Unit(1, "m").freeze >= Unit("0.9 m")true (no error)
  • Unit(60, "Hz").freeze < Unit("61 Hz")true (no error)
  • Unit(1, "kg").freeze == Unit("1 kg")true (no error)

@nertzy nertzy force-pushed the support-freeze branch 2 times, most recently from b0cd965 to efebced Compare June 17, 2026 21:04
@nertzy nertzy merged commit 5b0fa98 into minad:master Jun 17, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant